import { PetColor } from "../pet/info/PetColor";
import { GTextFormat } from "../../../common/GTextFormat";
import { Language } from "../../../../extend/language/Language";
import { FilterConst } from "../../../component/gconst/FilterConst";
import { PetAnimateItem } from "./PetAnimateItem";
import { GButton } from "../../../../com/mui/controls/GButton";
import { SPetInfo } from "../../../../Message/Game/SPetInfo";
import { PetUtil } from "../pet/info/PetUtil";
import { EventName } from "../../mvc/EventName";
import { Dispatcher } from "../../../mvc/core/Dispatcher";
import { DataEvent } from "../../events/DataEvent";
import { PetOpRegister } from "../common/menu/PetOpRegister";
import { PetOpMenuConst } from "../common/menu/PetOpMenuConst";
import { ResouceConst } from "../../../component/gconst/ResouceConst";
import { ImagesConst } from "../../resource/ImagesConst";
import { GImageBitmap } from "../../../../com/mui/controls/GImageBitmap";
import { Cache } from "../../cache/Cache";
import { _petUid } from "../../../../this/_petUid";
import { _order } from "../../../../this/_order";
type int = number;
//class PetWareHouseItem
    
    export  class PetWareHouseItem /* flash.display.Sprite */
    {
        constructor(arg1: int)
        {
            
            this.init();
            this._order = arg1;
            return;
        }

        /* internal  */createMenu(): void
        {
            if (this._operMenuSprite == null) 
            {
                this._operMenuSprite = new Object /* flash.display.Sprite */();
                this._operMenuSprite.x = 15;
                this._operMenuSprite.y = 130;
                this._operMenuSprite.graphics.beginFill(16711680, 0);
                this._operMenuSprite.graphics.drawRect(0, 0, 50, 40);
                addChild(this._operMenuSprite);
                this._operMenuSprite.buttonMode = true;
                this._operMenuSprite.addEventListener(flash.events.MouseEvent.CLICK, this.onOperMenuSpClickHandler);
            }
            return;
        }

        /* internal  */initTxt(): void
        {
            var loc1=0;
            var loc2=null;
            if (this._nameTxt == null) 
            {
                this._nameTxt = this.createTxt(15, 15, 80, 20);
            }
            if (this._geneTxt == null) 
            {
                this._geneTxt = this.createTxt(15, 105, 30, 20);
            }
            if (this._sexTxt == null) 
            {
                this._sexTxt = this.createTxt(95, 105, 40, 20);
            }
            if (this._petInfo != null) 
            {
                loc1 = PetColor.getTalentColorByTalent(this._petInfo.gene, this._petInfo.talent).intColor;
                loc2 = new GTextFormat("", 12, loc1);
                this._nameTxt.text = this._petInfo.name;
                this._nameTxt.setTextFormat(loc2);
                addChildAt(this._nameTxt, getChildIndex(this._petItem) + 1);
                if (this._petInfo.gene != 1) 
                {
                    if (this._petInfo.gene == 2) 
                    {
                        this._geneTxt.text = Language.getString(35107);
                    }
                }
                else 
                {
                    this._geneTxt.text = Language.getString(35106);
                }
                this._geneTxt.setTextFormat(loc2);
                addChildAt(this._geneTxt, getChildIndex(this._petItem) + 2);
                if (this._petInfo.sex != 0) 
                {
                    this._sexTxt.text = Language.getString(35109);
                }
                else 
                {
                    this._sexTxt.text = Language.getString(35108);
                }
                this._sexTxt.setTextFormat(loc2);
                addChildAt(this._sexTxt, getChildIndex(this._petItem) + 3);
            }
            return;
        }

        /* internal  */createTxt(arg1: int, arg2: int, arg3: int, arg4: int, arg5: Object /* flash.text.TextFormat */=null): Object /* flash.text.TextField */
        {
            var loc1=null;
            (loc1 = new Object /* flash.text.TextField */()).x = arg1;
            loc1.y = arg2;
            loc1.width = arg3;
            loc1.height = arg4;
            loc1.selectable = false;
            if (arg5 != null) 
            {
                loc1.defaultTextFormat = arg5;
            }
            loc1.filters = [FilterConst.glowFilter];
            addChild(loc1);
            return loc1;
        }

        /* internal  */onPetModleLoadCompleteHandler(arg1: Object /* flash.events.Event */): void
        {
            var loc4=0;
            var loc5=0;
            var loc1=arg1.target as PetAnimateItem;
            var loc2=loc1.width;
            var loc3=loc1.height;
            if (loc2 > 130) 
            {
                loc4 = loc2 / 2 - Math.abs(130 - loc2) / 2;
            }
            else 
            {
                loc4 = loc2 / 2 + Math.abs(130 - loc2) / 2;
            }
            if (loc3 > 130) 
            {
                loc5 = loc3 / 2 - Math.abs(130 - loc3) / 2 + 60;
            }
            else 
            {
                loc5 = loc3 / 2 + Math.abs(130 - loc3) / 2 + 40;
            }
            loc1.x = loc4;
            loc1.y = loc5;
            var loc6;
            (loc6 = new Object /* flash.display.Bitmap */()).bitmapData = new Object /* flash.display.BitmapData */(139, 165, true, 16777215);
            loc6.alpha = 0;
            addChild(loc6);
            loc1.mask = loc6;
            return;
        }

        /* internal  */createGButton(arg1: int, arg2: int, arg3: int, arg4: int, arg5: string, arg6: string): GButton
        {
            var loc1;
            (loc1 = new GButton()).x = arg1;
            loc1.y = arg2;
            loc1.label = arg5;
            loc1.width = arg3;
            loc1.height = arg4;
            loc1.styleName = arg6;
            this.addChild(loc1);
            return loc1;
        }

        public updateWareHouseItem(arg1: SPetInfo): void
        {
            this._data = arg1;
            this._petUid = arg1.uid;
            var loc1=PetUtil.getShowModelId(arg1);
            if (this._petItem) 
            {
                this._petItem.updateAnimate(loc1);
            }
            return;
        }

        public storePetAnimate(arg1: SPetInfo): void
        {
            this._data = arg1;
            this._petUid = arg1.uid;
            this._petItem = new PetAnimateItem();
            this._petItem.x = 70;
            this._petItem.y = 110;
            addChild(this._petItem);
            var loc1=PetUtil.getShowModelId(arg1);
            this._petItem.addEventListener(EventName.PetModleLoadComplete, this.onPetModleLoadCompleteHandler);
            this._petItem.updateAnimate(loc1);
            this._petInfo = arg1;
            this._isStore = true;
            if (this._singleBtn) 
            {
                this._singleBtn.removeEventListener(flash.events.MouseEvent.CLICK, this.onSingleBtnClickHandler);
                removeChild(this._singleBtn);
                this._singleBtn = null;
            }
            if (this._lookUpBtn == null) 
            {
                this._lookUpBtn = this.createGButton(15, 137, 50, 22, Language.getString(35110), "Button");
                this._lookUpBtn.addEventListener(flash.events.MouseEvent.CLICK, this.onLookUpBtnClickHandler);
            }
            if (this._takeOutBtn == null) 
            {
                this._takeOutBtn = this.createGButton(75, 137, 50, 22, Language.getString(35103), "Button");
                this._takeOutBtn.addEventListener(flash.events.MouseEvent.CLICK, this.onTakeOutBtnClickHandler);
            }
            if (this._operMenuSprite && !this._operMenuSprite.parent) 
            {
                addChild(this._operMenuSprite);
            }
            else 
            {
                this.createMenu();
            }
            this.initTxt();
            return;
        }

        public takeOutPetAnimate(arg1: SPetInfo): void
        {
            this.clearItem();
            if (this._singleBtn == null) 
            {
                this._singleBtn = this.createGButton(45, 137, 50, 22, Language.getString(35104), "Button");
                this._singleBtn.addEventListener(flash.events.MouseEvent.CLICK, this.onSingleBtnClickHandler);
            }
            return;
        }

        public clearItem(): void
        {
            this._data = null;
            this._petUid = null;
            this._petInfo = null;
            if (this._petItem) 
            {
                this._isStore = false;
                this._petItem.disposeAnimate();
                removeChild(this._petItem);
                this._petItem = null;
            }
            if (this._lookUpBtn) 
            {
                this._lookUpBtn.removeEventListener(flash.events.MouseEvent.CLICK, this.onLookUpBtnClickHandler);
                removeChild(this._lookUpBtn);
                this._lookUpBtn = null;
            }
            if (this._takeOutBtn) 
            {
                this._takeOutBtn.removeEventListener(flash.events.MouseEvent.CLICK, this.onTakeOutBtnClickHandler);
                removeChild(this._takeOutBtn);
                this._takeOutBtn = null;
            }
            if (this._singleBtn) 
            {
                this._singleBtn.removeEventListener(flash.events.MouseEvent.CLICK, this.onSingleBtnClickHandler);
                removeChild(this._singleBtn);
                this._singleBtn = null;
            }
            if (this._nameTxt) 
            {
                this._nameTxt.text = "";
            }
            if (this._geneTxt) 
            {
                this._geneTxt.text = "";
            }
            if (this._sexTxt) 
            {
                this._sexTxt.text = "";
            }
            if (this._operMenuSprite && this._operMenuSprite.parent) 
            {
                this._operMenuSprite.graphics.clear();
                this._operMenuSprite.removeEventListener(flash.events.MouseEvent.CLICK, this.onOperMenuSpClickHandler);
                removeChild(this._operMenuSprite);
                this._operMenuSprite = null;
            }
            if (this._shape) 
            {
                this._shape.graphics.clear();
                removeChild(this._shape);
                this._shape = null;
            }
            return;
        }

        public expendCapacity(): void
        {
            this._isOpen = true;
            if (this._singleBtn && this._singleBtn.parent) 
            {
                this._singleBtn.label = Language.getString(35104);
            }
            if (this._shape && this._shape.parent) 
            {
                this._shape.graphics.clear();
                removeChild(this._shape);
            }
            return;
        }

        /* internal  */onLookUpBtnClickHandler(arg1: Object /* flash.events.MouseEvent */): void
        {
            if (this._petUid) 
            {
                Dispatcher.dispatchEvent(new DataEvent(EventName.PetWareHouse_LookUpPet, this._petUid));
            }
            return;
        }

        /* internal  */onTakeOutBtnClickHandler(arg1: Object /* flash.events.MouseEvent */): void
        {
            var loc1=null;
            if (this._petUid) 
            {
                loc1 = {"petUid": _petUid, "order": _order};
                Dispatcher.dispatchEvent(new DataEvent(EventName.PetWareHouse_TakeOutPet, loc1));
            }
            return;
        }

        /* internal  */onSingleBtnClickHandler(arg1: Object /* flash.events.MouseEvent */): void
        {
            if (this._singleBtn.label != Language.getString(35104)) 
            {
                if (this._singleBtn.label == Language.getString(35105)) 
                {
                    Dispatcher.dispatchEvent(new DataEvent(EventName.PetWareHouse_ExpendCapacity, this._order));
                }
            }
            else 
            {
                Dispatcher.dispatchEvent(new DataEvent(EventName.PetWareHouse_StorePet, this._order));
            }
            return;
        }

        /* internal  */onOperMenuSpClickHandler(arg1: Object /* flash.events.MouseEvent */): void
        {
            PetOpRegister.UnRegister(this._operMenuSprite);
            PetOpRegister.Register(this._operMenuSprite, null, PetOpMenuConst.PetWarehouseOpMenu, this._data);
            return;
        }

        public disposeAnimateItem(): void
        {
            if (this._petItem) 
            {
                this._petItem.disposeAnimate();
            }
            return;
        }

        public setFilter(arg1: Boolean): void
        {
            if (arg1) 
            {
                this.filters = [FilterConst.gradientGlowFilter];
            }
            else 
            {
                this.filters = [];
            }
            return;
        }

        public set order(arg1: int)
        {
            this._order = arg1;
            return;
        }

        public get order(): int
        {
            return this._order;
        }

        public get isOpen(): Boolean
        {
            return this._isOpen;
        }

        public get isStore(): Boolean
        {
            return this._isStore;
        }

        public get petUid(): string
        {
            return this._petUid;
        }

        /* internal  */init(): void
        {
            var loc2=null;
            var loc1=ResouceConst.getScaleBitmap(ImagesConst.WindowCenterA);
            loc1.width = 139;
            loc1.height = 165;
            loc1.x = 0;
            loc1.y = 0;
            addChild(loc1);
            loc2 = ResouceConst.getScaleBitmap(ImagesConst.WindowCenterB);
            loc2.width = 131;
            loc2.height = 157;
            loc2.x = 4;
            loc2.y = 4;
            addChild(loc2);
            this._itemBg = new GImageBitmap(ImagesConst.PetWareHouseItemBg, 0, 0);
            addChild(this._itemBg);
            return;
        }

        public initItem(arg1: Boolean, arg2: SPetInfo=null): void
        {
            var loc1=0;
            this._isOpen = arg1;
            if (arg1) 
            {
                if (arg2) 
                {
                    loc1 = PetUtil.getShowModelId(arg2);
                    this._data = arg2;
                    this._petUid = arg2.uid;
                    this._petItem = new PetAnimateItem();
                    addChild(this._petItem);
                    this._isStore = true;
                    this._petItem.addEventListener(EventName.PetModleLoadComplete, this.onPetModleLoadCompleteHandler);
                    this._petItem.updateAnimate(loc1);
                    this._petInfo = Cache.instance.petWareHouse.getPetInfoByUid(this._petUid);
                    this._lookUpBtn = this.createGButton(15, 137, 50, 22, Language.getString(35110), "Button");
                    this._lookUpBtn.addEventListener(flash.events.MouseEvent.CLICK, this.onLookUpBtnClickHandler);
                    this._takeOutBtn = this.createGButton(75, 137, 50, 22, Language.getString(35103), "Button");
                    this._takeOutBtn.addEventListener(flash.events.MouseEvent.CLICK, this.onTakeOutBtnClickHandler);
                    this.initTxt();
                    this.createMenu();
                }
                else 
                {
                    this._singleBtn = this.createGButton(45, 137, 50, 22, Language.getString(35104), "Button");
                }
            }
            else 
            {
                this._shape = new Object /* flash.display.Shape */();
                this._shape.graphics.moveTo(5, 5);
                this._shape.graphics.lineStyle(0, 6710886, 0);
                this._shape.graphics.beginFill(6710886, 0.5);
                this._shape.graphics.lineTo(133, 5);
                this._shape.graphics.lineTo(133, 130);
                this._shape.graphics.lineTo(5, 130);
                this._shape.graphics.endFill();
                this._shape.cacheAsBitmap = true;
                addChild(this._shape);
                this._singleBtn = this.createGButton(45, 137, 50, 22, Language.getString(35105), "Button");
            }
            if (this._singleBtn) 
            {
                this._singleBtn.addEventListener(flash.events.MouseEvent.CLICK, this.onSingleBtnClickHandler);
            }
            return;
        }

        /* internal */ /* var */_petItem: PetAnimateItem;

        /* internal */ /* var */_itemBg: Object /* flash.display.Bitmap */;

        /* internal */ /* var */_lookUpBtn: GButton;

        /* internal */ /* var */_takeOutBtn: GButton;

        /* internal */ /* var */_singleBtn: GButton;

        /* internal */ /* var */_nameTxt: Object /* flash.text.TextField */;

        /* internal */ /* var */_geneTxt: Object /* flash.text.TextField */;

        /* internal */ /* var */_sexTxt: Object /* flash.text.TextField */;

        /* internal */ /* var */_shape: Object /* flash.display.Shape */;

        /* internal */ /* var */_data: SPetInfo;

        /* internal */ /* var */_order: int;

        /* internal */ /* var */_petUid: string;

        /* internal */ /* var */_isStore: Boolean=false;

        /* internal */ /* var */_petInfo: SPetInfo;

        /* internal */ /* var */_operMenuSprite: Object /* flash.display.Sprite */;

        /* internal */ /* var */_isOpen: Boolean;
    }
